package com.责任链模式;

import java.util.List;

/**
 * 实现chain的真正的包装Request和转发功能
 * @author Fang Ruichuan
 * @date 2022-09-03 12:50
 */
public class RealChain implements Ratify.Chain {

    public Request request;
    public List<Ratify> ratifyList;
    public int index;

    public RealChain(Request request, List<Ratify> ratifyList, int index) {
        this.request = request;
        this.ratifyList = ratifyList;
        this.index = index;
    }

    /**
     * 返回当前Request对象或者返回当前进行包装后的Request对象
     * @author Fang Ruichuan
     * @date 2022/9/3 12:52
     * @return Request
     */
    @Override
    public Request request() {
        return request;
    }

    /**
     * 具体转发功能
     * @author Fang Ruichuan
     * @date 2022/9/3 12:53
     * @param request
     * @return Result
     */
    @Override
    public Result proceed(Request request) {
        Result proceed = null;
        if (ratifyList.size() > index) {
            RealChain realChain = new RealChain(request, ratifyList, index+1);
            Ratify ratify = ratifyList.get(index);
            proceed = ratify.deal(realChain);
        }
        return proceed;
    }
}
